﻿using System;  
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

using TableTop.DataModel.Shelf;
using TableTop.Presentation.Controls;

namespace TableTop.Presentation.Controls
{

    [TemplatePart(Name= NamePart, Type = typeof(TextBox))]
    [TemplatePart(Name= LevelPart, Type =  typeof(TextBox))]
    [TemplatePart(Name= StrenghtPart, Type =  typeof(TextBox))]
    [TemplatePart(Name= DexterityPart, Type =  typeof(TextBox))]
    [TemplatePart(Name = IntellectPart, Type = typeof(TextBox))]
    [TemplatePart(Name = WisdomPart, Type = typeof(TextBox))]
    [TemplatePart(Name = ConstitutionPart, Type = typeof(TextBox))]
    [TemplatePart(Name = CharismaPart, Type = typeof(TextBox))]
    public class GRPGCharacterSheetEditor
        : CharacterSheetEditor
    {

        private const string NamePart = "txtName";
        private const string LevelPart = "txtLevel";
        private const string StrenghtPart = "txtStr";
        private const string DexterityPart = "txtDex";
        private const string IntellectPart = "txtInt";
        private const string WisdomPart = "txtWis";
        private const string ConstitutionPart = "txtCos";
        private const string CharismaPart = "txtCha";

        private TextBox txtName = null;
        private TextBox txtLevel = null;
        
        private CSGenericSheetData CharacterSheet
        {
            get;
            set;
        }

        public GRPGCharacterSheetEditor()
            : this(new CSGenericSheetData())
        {
        }

        public GRPGCharacterSheetEditor(CSGenericSheetData CharacterSheet)
        {
            this.CharacterSheet = CharacterSheet;
            this.DefaultStyleKey = typeof(GRPGCharacterSheetEditor);            
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        protected override void LocateControls()
        {
            base.LocateControls();
            
            this.txtName = this.Template.FindName(NamePart, this) as TextBox;
            this.txtLevel = this.Template.FindName(LevelPart, this) as TextBox;
        }

        protected override void RegisterControlsEvents()
        {
            base.RegisterControlsEvents();
        }

        protected override void OnSave()
        {
            // Checks data and eventually saves it.
            if (this.CharacterSheet != null)
            {                
                this.CharacterSheet.Name = this.txtName.Text;
                try
                {
                    this.CharacterSheet.Level = int.Parse(this.txtLevel.Text);
                }
                catch (FormatException)
                {
                    this.CharacterSheet.Level = 0;
                }
                this.CharacterSheet.SheetID = Guid.NewGuid();
                this.CharacterSheet.AddonID = AddonHelper.GUID;

                // Once data is set, we call save base
                base.OnSaveBase(this.CharacterSheet);
            }
        }
    }
}
